package com.yuneec.fly.models;


public class DroneTypeFactory {
    public static final int VEHICLE_TYPE_H920 = 1; // 6 motors
    public static final int VEHICLE_TYPE_Q500 = 2; // 4 motors
    public static final int VEHICLE_TYPE_350QX = 3; // 4 motors
    public static final int VEHICLE_TYPE_380QX = 4; // 4 motors
    public static final int VEHICLE_TYPE_480 = 5; // 6 motors

    public static DroneType getDrone(int type) {
        switch (type) {
            case VEHICLE_TYPE_H920:
                return new DroneTypeH920();
            case VEHICLE_TYPE_480:
                return new DroneType480();
            case VEHICLE_TYPE_Q500:
                return new DroneTypeQ500();
            default:
                return new DroneType();
        }
    }

    public static class DroneTypeH920 extends DroneType {
        private int droneType = VEHICLE_TYPE_H920;
        private float MAX_VOLTAGE = 25.2f;
        private float MID_VOLTAGE = 23.9f;
        private float LEVEL1_WARNING = 21.7f;
        private float LEVEL2_WARNING = 21.3f;
        private float MIN_VOLTAGE = 21.1f;
        private int mTmpVOLIntLevel = 0;

        @Override
        public int getDroneType() {
            return droneType;
        }

        @Override
        public int getVoltageLevel(float voltage) {
            //第一阶段：100%（25.2V ~ +∞）
            if (voltage >= MAX_VOLTAGE) {
                mTmpVOLIntLevel = 100;
                //第二阶段：95%~100%（23.9V ~ 25.2V）
            } else if (voltage >= MID_VOLTAGE && voltage < MAX_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - MID_VOLTAGE) * (100 - 95) / (MAX_VOLTAGE - MID_VOLTAGE) + 95);
                //第三阶段：20%~95%（21.7V ~ 23.9V）
            } else if (voltage >= LEVEL1_WARNING && voltage < MID_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL1_WARNING) * (95 - 20) / (MID_VOLTAGE - LEVEL1_WARNING) + 20);
                //第四阶段：5%~20%（21.3V ~ 21.7V）
            } else if (voltage >= LEVEL2_WARNING && voltage < LEVEL1_WARNING) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL2_WARNING) * (10 - 5) / (LEVEL1_WARNING - LEVEL2_WARNING) + 5);
                //第五阶段：0%~5%（21.1V ~ 21.3V）
            } else if (voltage >= MIN_VOLTAGE && voltage < LEVEL2_WARNING) {
                mTmpVOLIntLevel = Math.round((voltage - MIN_VOLTAGE) * (5 - 0) / (LEVEL2_WARNING - MIN_VOLTAGE));
                //第六阶段：-∞ ~ 0%（-∞ ~ 21.1V）
            } else if (voltage < MIN_VOLTAGE) {
                mTmpVOLIntLevel = 0;
            }
            return mTmpVOLIntLevel;
        }
    }

    public static class DroneType480 extends DroneType {
        private int droneType = VEHICLE_TYPE_480;
        private float MAX_VOLTAGE = 15.4f;
        private float MID_VOLTAGE = 14.9f;
        private float LEVEL1_WARNING = 14.2f;
        private float LEVEL2_WARNING = 14.0f;
        private int mTmpVOLIntLevel = 0;

        @Override
        public int getDroneType() {
            return droneType;
        }

        @Override
        public int getVoltageLevel(float voltage) {
            //第一阶段：100%（15.4V ~ +∞）
            if (voltage >= MAX_VOLTAGE) {
                mTmpVOLIntLevel = 100;
                //第二阶段：50%~75%（14.9V ~ 15.4V）
            } else if (voltage >= MID_VOLTAGE && voltage < MAX_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - MID_VOLTAGE) * (75 - 50) / (MAX_VOLTAGE - MID_VOLTAGE) + 50);
                //第三阶段：25%~50%（14.2V ~ 14.9V）
            } else if (voltage > LEVEL1_WARNING && voltage < MID_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL1_WARNING) * (50 - 25) / (MID_VOLTAGE - LEVEL1_WARNING) + 25);
                //第四阶段：0%~25%（14.0V ~ 14.2V）
            } else if (voltage > LEVEL2_WARNING && voltage <= LEVEL1_WARNING) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL2_WARNING) * (25 - 0) / (LEVEL1_WARNING - LEVEL2_WARNING));
                //第五阶段：0%（-∞ ~ 14.0V）
            } else if (voltage <= LEVEL2_WARNING) {
                mTmpVOLIntLevel = 0;
            }
            return mTmpVOLIntLevel;
        }
    }

    public static class DroneTypeQ500 extends DroneType {
        private int droneType = VEHICLE_TYPE_Q500;
        private float MAX_VOLTAGE = 12.9f;
        private float MID_VOLTAGE = 9.4f;
        private float LEVEL1_WARNING = 10.6f;
        private float LEVEL2_WARNING = 10.4f;
        private int mTmpVOLIntLevel = 0;

        @Override
        public int getDroneType() {
            return droneType;
        }

        @Override
        public int getVoltageLevel(float voltage) {
            //第一阶段：100%（15.4V ~ +∞）
            if (voltage >= MAX_VOLTAGE) {
                mTmpVOLIntLevel = 100;
                //第二阶段：50%~75%（14.9V ~ 15.4V）
            } else if (voltage >= MID_VOLTAGE && voltage < MAX_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - MID_VOLTAGE) * (75 - 50) / (MAX_VOLTAGE - MID_VOLTAGE) + 50);
                //第三阶段：25%~50%（14.2V ~ 14.9V）
            } else if (voltage > LEVEL1_WARNING && voltage < MID_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL1_WARNING) * (50 - 25) / (MID_VOLTAGE - LEVEL1_WARNING) + 25);
                //第四阶段：0%~25%（14.0V ~ 14.2V）
            } else if (voltage > LEVEL2_WARNING && voltage <= LEVEL1_WARNING) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL2_WARNING) * (25 - 0) / (LEVEL1_WARNING - LEVEL2_WARNING));
                //第五阶段：0%（-∞ ~ 14.0V）
            } else if (voltage <= LEVEL2_WARNING) {
                mTmpVOLIntLevel = 0;
            }
            return mTmpVOLIntLevel;
        }
    }

    public static class DroneType {
        private int droneType = VEHICLE_TYPE_H920;
        private float MAX_VOLTAGE = 25.2f;
        private float MID_VOLTAGE = 23.9f;
        private float LEVEL1_WARNING = 21.7f;
        private float LEVEL2_WARNING = 21.3f;
        private float MIN_VOLTAGE = 21.1f;
        private int mTmpVOLIntLevel = 0;

        public DroneType() {
        }

        public DroneType(int droneType) {
            this.droneType = droneType;
        }

        public int getDroneType() {
            return droneType;
        }

        public float getMaxVoltage() {
            return MAX_VOLTAGE;
        }

        public float getMinVoltage() {
            return MIN_VOLTAGE;
        }

        public int getVoltageLevel(float voltage) {
            //第一阶段：100%（25.2V ~ +∞）
            if (voltage >= MAX_VOLTAGE) {
                mTmpVOLIntLevel = 100;
                //第二阶段：95%~100%（23.9V ~ 25.2V）
            } else if (voltage >= MID_VOLTAGE && voltage < MAX_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - MID_VOLTAGE) * (100 - 95) / (MAX_VOLTAGE - MID_VOLTAGE) + 95);
                //第三阶段：10%~95%（21.7V ~ 23.9V）
            } else if (voltage >= LEVEL1_WARNING && voltage < MID_VOLTAGE) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL1_WARNING) * (95 - 10) / (MID_VOLTAGE - LEVEL1_WARNING) + 10);
                //第四阶段：5%~10%（21.4V ~ 21.7V）
            } else if (voltage >= LEVEL2_WARNING && voltage < LEVEL1_WARNING) {
                mTmpVOLIntLevel = Math.round((voltage - LEVEL2_WARNING) * (10 - 5) / (LEVEL1_WARNING - LEVEL2_WARNING) + 5);
                //第五阶段：0%~5%（21.1V ~ 21.3V）
            } else if (voltage >= MIN_VOLTAGE && voltage < LEVEL2_WARNING) {
                mTmpVOLIntLevel = Math.round((voltage - MIN_VOLTAGE) * (5 - 0) / (LEVEL2_WARNING - MIN_VOLTAGE));
                //第六阶段：-∞ ~ 0%（-∞ ~ 21.1V）
            } else if (voltage < MIN_VOLTAGE) {
                mTmpVOLIntLevel = 0;
            }
            return mTmpVOLIntLevel;
        }
    }
}
